package com.logica.mdm.webfrontend.embedding.theme;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.theme.AbstractThemeResolver;

import com.logica.mdm.webfrontend.embedding.EmbeddingAdapter;
import com.logica.mdm.webfrontend.embedding.EmbeddingResolver;

/**
 * Implementation of ThemeResolver that uses the embedding adapter's theme,
 * with a fallback to the default theme if undefined.
 * @author Fabien Crespel <fabien.crespel@logica.com>
 */
public class EmbeddingThemeResolver extends AbstractThemeResolver {
	
	@Autowired
	private EmbeddingResolver embeddingResolver;

	@Override
	public String resolveThemeName(HttpServletRequest request) {
		EmbeddingAdapter embeddingAdapter = embeddingResolver.resolveEmbeddingAdapter(request);
		String themeName = (embeddingAdapter != null) ? embeddingAdapter.getThemeName() : null;
		return (themeName != null ? themeName : getDefaultThemeName());
	}

	@Override
	public void setThemeName(HttpServletRequest request, HttpServletResponse response, String themeName) {
		throw new UnsupportedOperationException("Cannot change theme - use a different theme resolution strategy");
	}

}
